<?php

class controller_technic extends controller_theme
{

	public function __construct()
	{
		parent::__construct();
	}


	// 列表
	public function listing()
	{
		$category_id = get('category_id', 0, 'int');

		$model = bone::get_model('technic');
		$template = bone::get_template('technic.listing');
		$template->set_title('技术库');

		$limit = 30;
		$pagination = $template->get_pagination();
		$pagination->set_limit( $limit );
		$pagination->set_total($model->get_technic_sum('', $category_id));
		$pagination->set_page(request('page', 1, 'get', 'int'));
		$pagination->set_url('./?controller=technic&task=listing');

		$technics = $model->get_technics('', $category_id, $pagination->get_offset(), $limit);
		$categories = $model->get_categories();

		$template->set('category_id', $category_id);
		$template->set('technics', $technics);
		$template->set('categories', $categories);
		$template->set_data($this->get_data());
		$template->display();
	}

	// 搜索
	public function ajax_search()
	{
		$key = post('key', null);
		$category_id = post('category_id', 0, 'int');

		$model = bone::get_model('technic');
		$this->set_ajax('status', 0);
		$this->set_ajax('total', $model->get_technic_sum($key, $category_id) );
		$this->ajax();
	}

	public function ajax_search_data()
	{
		$key = post('key', null);
		$category_id = post('category_id', 0, 'int');
		$offset = post('offset', 0, 'int');
		$limit = post('limit', 30, 'int');

		$model = bone::get_model('technic');
		$this->set_ajax('status', 0);
		$technics = $model->get_technics($key, $category_id, $offset, $limit);
		$categories = $model->get_categories();
		foreach($technics as $technic)
		{
			$technic->url = url('./?controller=technic&task=popup_detail&technic_id='.$technic->id );
			$technic->category_name = $categories[$technic->category_id]->name;
			$technic->create_time = date('Y-m-d H:i', $technic->create_time);
		}
		$this->set_ajax('technics', $technics );
		$this->ajax();
	}

	// 详情
	public function detail()
	{
		$technic_id = get('technic_id', 0, 'int');
		if(!$technic_id) exit('变量{technic_id}缺失');
		$popup = get('popup', 0, 'int');

		$technic = bone::get_table('technic');
		$technic->load($technic_id);
		$technic->hit();

		$technic_category = bone::get_table('technic_category');
		$technic_category->load($technic->category_id);
		
		$template = bone::get_template('technic.detail');
		$template->set_title($technic->title);
		$template->set_meta_keyword( $technic->keyword );
		$template->set_meta_description( $technic->summary );
		$template->set('technic', $technic);
		$template->set('technic_category', $technic_category);
		$template->set_data($this->get_data());
		$template->display();
	}

	// 详情
	public function popup_detail()
	{
		$technic_id = get('technic_id', 0, 'int');
		if(!$technic_id) exit('变量{technic_id}缺失');

		$technic = bone::get_table('technic');
		$technic->load($technic_id);
		$technic->hit();
		
		$template = bone::get_template('technic.popup_detail');
		$template->set_title($technic->title);
		$template->set('technic', $technic);
		$template->set_data($this->get_data());
		$template->display();
	}

}
?>